package com.industrial.premu.controller.chart;

import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.ann.BuilderVo;
import com.industrial.premu.domain.PreMuBenefitEvaluation;
import com.industrial.premu.dto.PreMuChartsDto;
import com.industrial.premu.service.IPreMuEnvironmentalIndicatorAnalysisService;
import com.industrial.premu.vo.PreMuChartsVo;
import com.industrial.system.api.domain.SysUser;
import com.industrial.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


/**
 * <p>
 *     环保指标分析
 * </p>
 * @since 2024-02-01
 * @author SangGuangCan
 */
@RestController
@RequestMapping("/eia")
public class PreMuEnvironmentalIndicatorAnalysisController extends BaseController {

    @Autowired
    private IPreMuEnvironmentalIndicatorAnalysisService preMuEnvironmentalIndicatorAnalysisService;

    /**
     * <p>
     *     查询近5年的数据
     * </p>
     * @param preChartsDto
     * @return
     */
    @GetMapping("/jwnsj")
    public AjaxResult queryDataFromThePastFiveYears(PreMuChartsDto preChartsDto){
        int years=Integer.valueOf(preChartsDto.getYears());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();

        SysUser sysUser = loginUser.getSysUser();
        if(sysUser.isAdmin()){
            preChartsDto.setProvince("370000");
        }else{
            if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
                String county=  loginUser.getSysUser().getDistrict();
                preChartsDto.setCounty(county);
            }else if(roles.contains("s_01_011")){
                String city=  loginUser.getSysUser().getCity();
                preChartsDto.setCity(city);
            }else if(roles.contains("p_01_011")){
                String province=  loginUser.getSysUser().getProvince();
                preChartsDto.setProvince(province);
            }
        }

        if(years>=2019){
            preChartsDto.setEndYears(String.valueOf(years));
            int  startYears=years-4;
            if(startYears>2019){
                preChartsDto.setStartYears(String.valueOf(startYears));
            }else{
                preChartsDto.setStartYears("2019");
            }
            preChartsDto.setYears("");
        }
        List<PreMuChartsVo> chartsVoList= new ArrayList<>();
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
            List<PreMuChartsVo> cList = preMuEnvironmentalIndicatorAnalysisService.groupEconomyByCityAndCounty(preChartsDto);
            List<PreMuChartsVo> pList=  preMuEnvironmentalIndicatorAnalysisService.groupEconomyByProvince(preChartsDto);

            for (int i = 0; i < pList.size(); i++) {
                PreMuChartsVo preMuChartsVo=new PreMuChartsVo();
                preMuChartsVo.setYears(pList.get(i).getYears());
                preMuChartsVo.setPIndexNum(pList.get(i).getPIndexNum());
                chartsVoList.add(preMuChartsVo);
            }
            for (int i = 0; i < cList.size(); i++) {
                for (int j = 0; j < chartsVoList.size(); j++) {
                    if(cList.get(i).getYears().contains(chartsVoList.get(j).getYears())){
                        chartsVoList.get(j).setCIndexNum(cList.get(i).getCIndexNum());
                    }
                }
            }
        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            preChartsDto.setCity(city);
            List<PreMuChartsVo> cList = preMuEnvironmentalIndicatorAnalysisService.groupEconomyByCityAndCounty(preChartsDto);
            List<PreMuChartsVo> pList=  preMuEnvironmentalIndicatorAnalysisService.groupEconomyByProvince(preChartsDto);

            for (int i = 0; i < pList.size(); i++) {
                PreMuChartsVo preMuChartsVo=new PreMuChartsVo();
                preMuChartsVo.setYears(pList.get(i).getYears());
                preMuChartsVo.setPIndexNum(pList.get(i).getPIndexNum());
                chartsVoList.add(preMuChartsVo);
            }
            for (int i = 0; i < cList.size(); i++) {
                for (int j = 0; j < chartsVoList.size(); j++) {
                    if(cList.get(i).getYears().contains(chartsVoList.get(j).getYears())){
                        chartsVoList.get(j).setCIndexNum(cList.get(i).getCIndexNum());
                    }
                }
            }
        }else if(roles.contains("p_01_011")){
            preChartsDto.setProvince("370000");
            List<PreMuChartsVo> pList=  preMuEnvironmentalIndicatorAnalysisService.groupEconomyByProvince(preChartsDto);
            for (int i = 0; i < pList.size(); i++) {
                PreMuChartsVo preMuChartsVo=new PreMuChartsVo();
                preMuChartsVo.setYears(pList.get(i).getYears());
                preMuChartsVo.setPIndexNum(pList.get(i).getPIndexNum());
                chartsVoList.add(preMuChartsVo);
            }
        }else if(roles.contains("admin")){
            preChartsDto.setProvince("370000");
            List<PreMuChartsVo> pList=  preMuEnvironmentalIndicatorAnalysisService.groupEconomyByProvince(preChartsDto);
            for (int i = 0; i < pList.size(); i++) {
                PreMuChartsVo preMuChartsVo=new PreMuChartsVo();
                preMuChartsVo.setYears(pList.get(i).getYears());
                preMuChartsVo.setPIndexNum(pList.get(i).getPIndexNum());
                chartsVoList.add(preMuChartsVo);
            }
        }
//                List<PreMuChartsVo> chartsVoList= preMuEnvironmentalIndicatorAnalysisService.groupEconomyByYears(preChartsDto);
        return AjaxResult.success(chartsVoList);
    }

    /**
     * 查询区域排名情况
     * @param preMuChartsDto
     * @return
     */
    @BuilderVo(require = {"years","companyType","indexName"})
    @GetMapping("/queryIndexAreaOrder")
    public TableDataInfo queryIndexAreaOrderTable(PreMuChartsDto preMuChartsDto){
        Integer yearInteger = Integer.valueOf(preMuChartsDto.getYears());
        if(yearInteger - 5 < 2019){
            preMuChartsDto.setStartYears(String.valueOf(2019));
        }else{
            preMuChartsDto.setStartYears(String.valueOf(yearInteger - 5));
        }
        preMuChartsDto.setEndYears(String.valueOf(yearInteger));

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();

        SysUser sysUser = loginUser.getSysUser();
        if(sysUser.isAdmin()){
            preMuChartsDto.setProvince("370000");
        }else{
            if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
                String county=  loginUser.getSysUser().getDistrict();
                preMuChartsDto.setCounty(county);
            }else if(roles.contains("s_01_011")){
                String city=  loginUser.getSysUser().getCity();
                preMuChartsDto.setCity(city);
            }else if(roles.contains("p_01_011")){
                String province=  loginUser.getSysUser().getProvince();
                preMuChartsDto.setProvince(province);
            }
        }

        startPage();
        List<PreMuBenefitEvaluation> preMuBenefitEvaluations = preMuEnvironmentalIndicatorAnalysisService.queryIndexAreaOrderTable(preMuChartsDto);
        return getDataTable(preMuBenefitEvaluations);
    }

    /**
     * 查询区域排名情况
     * @param preMuChartsDto
     * @return
     */
    @BuilderVo(require = {"years","companyType","indexName"})
    @GetMapping("/queryIndexAreaEcharts")
    public AjaxResult queryIndexAreaOrderEcharts(PreMuChartsDto preMuChartsDto){
        Integer yearInteger = Integer.valueOf(preMuChartsDto.getYears());
        if(yearInteger - 5 < 2019){
            preMuChartsDto.setStartYears(String.valueOf(2019));
        }else{
            preMuChartsDto.setStartYears(String.valueOf(yearInteger - 5));
        }
        preMuChartsDto.setEndYears(String.valueOf(yearInteger));

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        SysUser sysUser = loginUser.getSysUser();
        if(sysUser.isAdmin()){
            preMuChartsDto.setProvince("370000");
        }else{
            if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
                String county=  loginUser.getSysUser().getDistrict();
                preMuChartsDto.setCounty(county);
            }else if(roles.contains("s_01_011")){
                String city=  loginUser.getSysUser().getCity();
                preMuChartsDto.setCity(city);
            }else if(roles.contains("p_01_011")){
                String province=  loginUser.getSysUser().getProvince();
                preMuChartsDto.setProvince(province);
            }
        }
        return preMuEnvironmentalIndicatorAnalysisService.queryIndexAreaOrderEcharts(preMuChartsDto);
    }

    /**
     * <p>
     *     查询行业排名图表
     * </p>
     * @return
     */
    @BuilderVo(require = {"years","companyType","indexName"})
    @GetMapping("/queryIndexHyOrderEcharts")
    public AjaxResult queryIndexHyOrderEcharts(PreMuChartsDto preMuChartsDto){


        preMuChartsDto.setPageNum(0);
        preMuChartsDto.setPageSize(9);
        preMuChartsDto.setPageFlag("1");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        SysUser sysUser = loginUser.getSysUser();
        if(sysUser.isAdmin()){
            preMuChartsDto.setProvince("370000");
        }else{
            if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
                String county=  loginUser.getSysUser().getDistrict();
                preMuChartsDto.setCounty(county);
            }else if(roles.contains("s_01_011")){
                String city=  loginUser.getSysUser().getCity();
                preMuChartsDto.setCity(city);
            }else if(roles.contains("p_01_011")){
                String province=  loginUser.getSysUser().getProvince();
                preMuChartsDto.setProvince(province);
            }
        }
        return preMuEnvironmentalIndicatorAnalysisService.queryIndexHyOrderEcharts(preMuChartsDto);
    }

    /**
     * <p>
     *     查询行业排名表格
     * </p>
     * @return
     */
    @BuilderVo(require = {"years","companyType","indexName"})
    @GetMapping("/queryIndexHyOrderTable")
    public TableDataInfo queryIndexHyOrderTable(PreMuChartsDto preMuChartsDto){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        SysUser sysUser = loginUser.getSysUser();
        if(sysUser.isAdmin()){
            preMuChartsDto.setProvince("370000");
        }else{
            if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
                String county=  loginUser.getSysUser().getDistrict();
                preMuChartsDto.setCounty(county);
            }else if(roles.contains("s_01_011")){
                String city=  loginUser.getSysUser().getCity();
                preMuChartsDto.setCity(city);
            }else if(roles.contains("p_01_011")){
                String province=  loginUser.getSysUser().getProvince();
                preMuChartsDto.setProvince(province);
            }
        }
        startPage();
        List<PreMuBenefitEvaluation> preMuBenefitEvaluations = preMuEnvironmentalIndicatorAnalysisService.queryIndexHyOrderTable(preMuChartsDto);
        return getDataTable(preMuBenefitEvaluations);
    }
}
